var amapFile = require('../../libs/amap-wx.130.js');

Page({
  data: {
    markers: [],
    distance: '',
    cost: '',
    polyline: []
  },
  onLoad: function(e) {
    var that = this;
    that.setData({
      markers:[
        {
          id: 0,
          latitude: getApp().globalData.origina,
          longitude: getApp().globalData.originb,
          width: 23,
          height: 33
        },{
          id: 0,
          latitude: getApp().globalData.desa,
          longitude: getApp().globalData.desb,
          width: 24,
          height: 34
        }
      ],
      latitude:getApp().globalData.origina,
      longitude:getApp().globalData.originb
    })
    var myAmapFun = new amapFile.AMapWX({key: '038c54183637f97bf87484c0ce1c7b01'});
        let origin=getApp().globalData.originb+','+getApp().globalData.origina
        let des=getApp().globalData.desb+','+getApp().globalData.desa
        console.log('des',des)
        console.log('origin',origin)
        myAmapFun.getDrivingRoute({
          origin:origin,
          destination:des,
          success: function(data){
            console.log(data)
            var points = [];
            if(data.paths && data.paths[0] && data.paths[0].steps){
              var steps = data.paths[0].steps;
              for(var i = 0; i < steps.length; i++){
                var poLen = steps[i].polyline.split(';');
                for(var j = 0;j < poLen.length; j++){
                  points.push({
                    longitude: parseFloat(poLen[j].split(',')[0]),
                    latitude: parseFloat(poLen[j].split(',')[1])
                  })
                } 
              }
            }
            that.setData({
              polyline: [{
                points: points,
                color: "#0091ff",
                width: 6
              }]
            });
            if(data.paths[0] && data.paths[0].distance){
              that.setData({
                distance: data.paths[0].distance + '米'
              });
            }
            if(data.taxi_cost){
              that.setData({
                cost: '打车约' + parseInt(data.taxi_cost) + '元'
              });
            }
          },
          fail: function(info){
            console.log(info)
          }
        })
      },
  goDetail: function(){
    wx.navigateTo({
      url: '../cardetail/cardetail'
    })
  },
  goToCar: function (e) {
    wx.redirectTo({
      url: '../321/321?originlati='+this.data.markers[0].latitude+'&originlongi='+this.data.markers[0].longitude+'&deslati='+this.data.markers[1].latitude+'&deslongi='+this.data.markers[1].longitude
    })
  },
  goToBus: function (e) {
    if(getApp().globalData.descity==getApp().globalData.origincity){
      wx.redirectTo({
        url: '../bus/bus?'
      })
    }
    else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title:'仅支持同城公交路线规划'
      })
    }
    
  },
  goToRide: function (e) {
    let distance=this.data.distance.substring(0,this.data.distance.length-1)
    if(distance<=500000){
      wx.redirectTo({
        url: '../ride/ride'
      })
    }
    else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title: '距离较远不建议骑行'
      })
    }
  },
  goToWalk: function (e) {
    let distance=this.data.distance.substring(0,this.data.distance.length-1)
    if(distance<=100000){
      wx.redirectTo({
        url: '../walk/walk?'
      })
    }
    else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title: '距离较远不建议步行'
      })
    }
  }
})